package com.rr.graphql.resolver;

import com.netflix.graphql.dgs.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.RequestHeader;

/**
 * @author RR
 * @date 2023/6/24 11:59
 */
@DgsComponent
@Slf4j
public class HelloGraphQLResolver {
    @DgsQuery(field = "helloGraphQL")
    public String helloGraphQL(DgsDataFetchingEnvironment environment){
        return "hello graphql";
    }

    @DgsQuery(field = "helloGraphQLWithException")
    public String helloGraphQLWithException(DgsDataFetchingEnvironment environment){
        throw new RuntimeException("抛出异常");
    }

    @DgsQuery(field = "repeat")
    public String repeat(@InputArgument(name = "context") String context) {
        return context;
    }

    @DgsQuery(field = "query1")
    public String query1() {
        log.info("query1");
        return "query1";
    }

    @DgsQuery(field = "query2")
    public String query2() {
        log.info("query2");
        return "query2";
    }

    @DgsMutation(field = "mutation1")
    public String mutation1() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return "mutation";
    }
    @DgsMutation(field = "mutation2")
    public String mutation2() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return "mutation";
    }

    @DgsQuery(field = "getHeader")
    public String getHeader(@RequestHeader String host, @RequestHeader String diy) {
        return "host：" + host + "，diy：" + diy;
    }
}
